package com.hhq.demo.wechat.controller;

import com.hhq.demo.bean.Express;
import com.hhq.demo.bean.ExpressList;
import com.hhq.demo.bean.User;
import com.hhq.demo.mvc.ResponseBody;
import com.hhq.demo.mvc.ResponseView;
import com.hhq.demo.service.ExpressService;
import com.hhq.demo.service.impl.ExpressServiceImpl;
import com.hhq.demo.utils.DateFormatUtil;
import com.hhq.demo.utils.JSONUtil;
import com.hhq.demo.utils.Message;
import com.hhq.demo.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HHQ
 * @date 2021年12月18日
 */
public class QrCodeController {

    ExpressService expressService = new ExpressServiceImpl();

    @ResponseView("/wx/createQRCode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        String type = request.getParameter("type");
        String qrContent = null;
        if ("user".equals(type)) {
            //用户个人的二维码
            User wxUserInfo = UserUtil.getWxUserInfo(request.getSession());
            qrContent = "user_" + wxUserInfo.getPhone();
        } else {
            //快递的二维码
            qrContent = "code_" + code;
        }
        request.getSession().setAttribute("qrContent", qrContent);
        return "/wechat/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQrcode(HttpServletRequest request, HttpServletResponse response) {
        String qrContent = (String) request.getSession().getAttribute("qrContent");
        Message message = new Message();
        if (qrContent != null) {
            message.setStatus(1);
            message.setResult(qrContent);
        } else {
            message.setStatus(0);
            message.setResult("二维码获取错误，请重新操作");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wx/userExpressList.do")
    public String userExpressList(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userPhone");
        List<Express> userExpressList = expressService.findByPhone(userPhone);
        //最终返回的快递格式
        List<ExpressList> returnUserExpress = new ArrayList<>();
        for (Express e : userExpressList) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "未出库" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "确认取件" : "已取件";
            String code = e.getCode() == null ? "已取件" : e.getCode();
            ExpressList e2 = new ExpressList(e.getId(), e.getNumber(), e.getCompany(), e.getReceiveName(), e.getReceivePhone(), e.getRecordPhone(), status, code, inTime, outTime);
            returnUserExpress.add(e2);
        }
        Message message = new Message();
        if (userExpressList.size() > 0) {
            message.setStatus(1);
            message.setResult("查找成功");
            message.setData(returnUserExpress);
        } else {
            message.setStatus(0);
            message.setResult("该用户暂无快递！");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/updateStatusByQrcode.do")
    public String updateStatus(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        int i = expressService.updateStatus(code);
        Message message = new Message();
        if (i > 0) {
            message.setStatus(1);
            message.setResult("取件成功");
        } else {
            message.setStatus(0);
            message.setResult("该快递不存在");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/wechat/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        Express e = expressService.findByCode(code);
        ExpressList e2 = null;
        if (e != null) {
            e2 = new ExpressList(e.getId(), e.getNumber(), e.getCompany(), e.getReceiveName(), e.getReceivePhone(), e.getRecordPhone(), e.getStatus()==0?"确认取件":"已取件", code, DateFormatUtil.format(e.getInTime()), e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()));
        }
        Message msg = new Message();
        if (e == null) {
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e2);
        }
        return JSONUtil.toJSON(msg);
    }
}
